/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function () {
    var url = '/v1/manager/grow/rules.json';

    var type = $('.nav-tabs a').attr('data-id');

    var toolbar = '';

    loadList(url, toolbar, type);

    $(document).on({
        change: function () {
            refreshList();
        }
    },'.select-status');

    $('.nav-tabs a').on('click', function () {
        if ($(this).attr('data-id') != type) {
            type = $(this).attr('data-id');
            if (type == 1) {
                $('#grow-user .row .col-xs-12').html('<table class="table table-hover data-table-user" data-height="600"></table>');
            } else {
                $('#grow-department .row .col-xs-12').html('<table class="table table-hover data-table-department" data-height="600"></table>');
            }
            loadList(url, toolbar, type);
        }
    });

    $(document).on('click', '.btn-delete', function () {
        deleteData('/v1/manager/delete/grow/rule', $(this).attr('data-id'));
    });

});

function loadList(url, toolbar, type) {
    var selector = type == 1 ? '.data-table-user' : '.data-table-department';
    $(selector).bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        toolbar: toolbar,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        search: false,
        sortable: false,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        queryParams : function (params) {
            params.type = type;
            return params;
        },
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: '序号'
        }, {
            field: 'title',
            title: '标题'
        }, {
            field: 'realname',
            title: '发布人'
        },  {
            field: 'created_at',
            title: '发布时间'
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value) {
                var action = '<a class="btn btn-sm btn-info" href="/manager/grow-edit-'+value+'.html"><i class="fa fa-edit"></i>编辑</a>';
                action += '<a class="btn btn-sm btn-danger btn-delete" href="javascript:;" data-id="'+value+'"><i class="fa fa-close">删除</a>';
                return action;
            }
        }]
    });
}

function refreshList() {
    $('.data-table').bootstrapTable('refresh');
}